package MAgentAssignment.gui;

import MAgentAssignment.ontology.concepts.Airport;
import MAgentAssignment.ontology.concepts.Flight;
import MAgentAssignment.ontology.concepts.Location;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Prageeth
 * Date: 2/16/12
 * Time: 8:01 PM
 * To change this template use File | Settings | File Templates.
 */
public class FlightIcon extends JComponent
{
    private Flight flight;
    private Location locationFrom;
    private Location locationTo;
    private Vector<FlightTravelListener> flightTravelListeners = new Vector<FlightTravelListener>();


    public void init()
    {
        flight = new Flight( "UL121" );
        locationFrom = new Location( 0, 0 );
        locationTo = new Location( 0, 0 );
    }

    public FlightIcon()
    {
        this.setSize( new Dimension( 10, 10 ) );
        this.setMinimumSize( new Dimension( 10, 10 ) );
        this.setMaximumSize( new Dimension( 10, 10 ) );
        setBackground( Color.BLACK );
        setForeground( Color.BLACK );
    }

    @Override public void paint( Graphics g )
    {
        //        super.paint( g );
        g.setColor( Color.BLACK );
        if( locationFrom != null && ( locationFrom.getLocX() != locationTo.getLocX() || locationFrom.getLocY() != locationTo.getLocY() ) )
        {
            g.drawOval( locationFrom.getLocX(), locationFrom.getLocY(), FlightUtil.FLIGH_ICON_WIDTH, FlightUtil.FLIGH_ICON_HEIGHT );
            //            g.drawOval( locationTo.getLocX(), locationTo.getLocY(), FlightUtil.AIRPORT_ICON_WIDTH, FlightUtil.AIRPORT_ICON_HEIGHT );
            if( flight != null )
            {
                g.drawString( flight.getFromAirport().getAirpotCode() + ":" + flight.getToAirport().getAirpotCode() + "-" + flight.getFlightNo() + " : ", locationFrom.getLocX() + FlightUtil.FLIGH_ICON_WIDTH, locationFrom.getLocY() + FlightUtil.FLIGH_ICON_WIDTH );
            }
        }

    }

    public void moveStep()
    {
        int xdiff = locationTo.getLocX() - locationFrom.getLocX();
        int ydiff = locationTo.getLocY() - locationFrom.getLocY();
        if( xdiff == 0 && ydiff == 0 && !flight.isLanded() )
        {
            flight.setLanded( true );
            fireArrival(false);
            return;
        }
        if( xdiff > 0 )
        {
            locationFrom.setLocX( locationFrom.getLocX() + ( xdiff > FlightUtil.FLIGH_ICON_WIDTH / 2 ? FlightUtil.FLIGH_ICON_WIDTH / 2 : xdiff ) );
        }
        else
        {
            locationFrom.setLocX( locationFrom.getLocX() + ( xdiff < FlightUtil.FLIGH_ICON_WIDTH / 2 * -1 ? FlightUtil.FLIGH_ICON_WIDTH / 2 * -1 : xdiff ) );
        }
        if( ydiff > 0 )
        {
            locationFrom.setLocY( locationFrom.getLocY() + ( ydiff > FlightUtil.FLIGH_ICON_HEIGHT / 2 ? FlightUtil.FLIGH_ICON_HEIGHT / 2 : ydiff ) );
        }
        else
        {
            locationFrom.setLocY( locationFrom.getLocY() + ( ydiff < -1 * FlightUtil.FLIGH_ICON_HEIGHT / 2 ? -1 * FlightUtil.FLIGH_ICON_HEIGHT / 2 : ydiff ) );
        }


    }

    private void fireArrival( boolean clear )
    {
        if( clear )
        {
            flightTravelListeners.clear();
        }
        for(int i = 0 ;i < flightTravelListeners.size() ; i++)
        {
            flightTravelListeners.get(i).arrived( this );
        }
    }

    public void cancelFlight()
    {
        fireArrival( true );
    }

    public Flight getFlight()
    {
        return flight;
    }

    public void setFlight( Flight flight )
    {
        this.flight = flight;
    }

    public Location getLocationFrom()
    {
        return locationFrom;
    }

    public void setLocationFrom( Location locationFrom )
    {
        this.locationFrom = locationFrom;
    }

    public Location getLocationTo()
    {
        return locationTo;
    }

    public void setLocationTo( Location locationTo )
    {
        this.locationTo = locationTo;
    }

    public void addTravelListener( FlightTravelListener flightTravelListener )
    {
        flightTravelListeners.add( flightTravelListener );
    }

    public void removeTravelListener( FlightTravelListener flightTravelListener )
    {
        flightTravelListeners.remove( flightTravelListener );
    }
}
